<template>
  <div class="data-dictionary common-style">
  	<div class="data-dictionary-header">
  		<div class="fl">
  			<select class="select" v-model="parentId">
					<option value="1">地区分类</option>
					<option value="2">政策类型</option>
					<option value="3">性质分类</option>
					<option value="4">税保协同</option>
					<option value="5">股东分红</option>
					<option value="6">大额资产处置所得</option>
					<option value="7">税收优惠政策</option>
					<option value="9">问题类型</option>
				</select>
  			<input class="input" v-model="searchValue">
  			<button class="Public-btn" @click="searchList()">搜索</button>
  		</div>
  		<button class="Public-btn fr" @click="addVal('1','')">添加值</button>
  	</div>
  	<div class="table-father"> 
  		<table class="table">
				<thead>
					<!--<th>序号</th>
					<th>值ID</th>-->
					<th>值</th>
					<th>类型</th>
					<th>排序号</th>
					<th>操作</th>
				</thead>
				<tbody>
					<tr v-for="(item,index) in tableList">
						<!--<td><span>{{index+1}}</span></td>
						<td><span>{{item.id}}</span></td>-->
						<td><span>{{item.value}}</span></td>
						<td>
							<span v-if="item.parentId==1">地区分类</span>
							<span v-if="item.parentId==2">政策类型</span>
							<span v-if="item.parentId==3">性质分类</span>
							<span v-if="item.parentId==4">税保协同</span>
							<span v-if="item.parentId==5">股东分红</span>
							<span v-if="item.parentId==6">大额资产处置所得</span>
							<span v-if="item.parentId==7">税收优惠政策</span>
							<span v-if="item.parentId==9">问题类型</span>
						</td>
						<td><span>{{item.orderNumber}}</span></td>
						<td>
							<button class="Public-btn" @click="addVal('2',item)">修改</button>
							<button class="Public-btn" @click="del(item.id)">删除</button>
						</td>
					</tr>
					<tr>
						<td colspan="4" style="color: red" v-if="!tableList.length">暂无数据</td>
					</tr>
				</tbody>
			</table>
  	</div>
    
		<Pageination  :total="model.total" :size="model.size" :page="model.page" :changge="pageFn"></Pageination>
  	<!--添加值-->
    	<div id="addValue" class="property_modal">
				<div class="modal-content" >
					<div class="header-text">
						<p v-if="currentIndex==1">添加值</p>
						<p v-if="currentIndex==2">修改值</p>
						<span @click="OffModeBoxs()">x</span>
					</div>
					<div class="section-nav">
						<p>
							<span><span>*</span>类型名称</span>
							<select class="select" v-model="data.parentId" name="parentId">
								<option value="1">地区分类</option>
								<option value="2">政策类型</option>
								<option value="3">性质分类</option>
								<option value="4">税保协同</option>
								<option value="5">股东分红</option>
								<option value="6">大额资产处置所得</option>
								<option value="7">税收优惠政策</option>
								<option value="9">问题类型</option>
							</select>
						</p>
						<p>
							<span><span>*</span>值名称</span>
							<input class="input" v-model="data.value" name="value" v-on:blur="blurInput()"/>
						</p>
						<p>
							<span>排序号</span>
							<input class="input"  v-model="data.orderNumber"/>
						</p>
						<div class="btns">
							<button class="Public-btn" @click="addFn()" v-if="currentIndex==1">添加</button>
						  <button class="Public-btn" @click="addFn()" v-if="currentIndex==2">修改</button>
						</div>
					</div>
				</div>
			</div>
  </div>
</template>
<script>
	import Pageination from "../shareComponent/pageination";
	export default {
    name:'data-dictionary',
    data:function(){
	    return{
	      tableList:[],
	      /*下拉列表id*/
	     	parentId:"1",
	     	/*关键字*/
	    	searchValue:"",
	    	data:{
	    		parentId:"1",
	    		value:"",
	    		orderNumber:"",
	    		"code": "userAdd"
	    	},
	    	/**分页初始化**/
		    model: {
	        total: 0,//总数
	        size: 10,//每页显示条目个数不传默认20
	        page: 1,//当前页码
		    },
	    	/*当前是新增1 修改2*/
	    	currentIndex:1,
	    	/*新增修改接口地址*/
	    	url:"",
	    	/*当前修改id*/
	    	id:"",
	    	/*修改时回显值名称*/
	    	currValue:"",
	    	/*当前页码*/
	    	currentPage:1,
      }
    },
    created: function(){
    	$(window).scrollTop(0);
    },
    components:{Pageination},
    methods:{
    	/*添加/修改值时验证值是否存在*/
    	blurInput:function(){
    		if(!this.data.value){
    			return;
    		}
    		if(this.data.value.length>20){
    			layer.alert("值名称最多只能输入20个字");
    			return;
    		}
    		
    		this.$post(
    			'/system/queryData ',
    			{
    				parentId:this.data.parentId,
    				value:this.data.value
    			}
    		)
    		.then(response => {
    			/*未登录重定向*/
          if(response==444){
          	this.$router.push({path: '/login'});
          }else{
          	if (response.resultCode ==0) {
          		if(response.resultData.length){
          			if(this.currentIndex==1){
	          			layer.alert("值名称已存在");
	          		}else if(this.currentIndex==2&&response.resultData[0].value!=this.currValue){
	          			layer.alert("值名称已存在");
	          		}
          		}
	          }
          }
         
        }).catch(err => {
          console.log(err);
        });
    	},
			/*添加值模态框显示*/
			addVal:function(index,item){
				this.currentIndex=index;
	    	if(index==1){
	    		this.data={
		    		parentId:"1",
		    		value:"",
		    		orderNumber:"",
		    	}
	    	}else{
	    		this.data={
		    		parentId:item.parentId,
		    		value:item.value,
		    		orderNumber:item.orderNumber,
		    	};
		    	this.currValue=item.value;
	    		this.id=item.id;
	    	}
				$("#addValue").css("display","block");
			},
			/*弹框关闭*/
			OffModeBoxs:function () {
        $(".property_modal").css("display","none");
    	},
    	//是否为正整数
    	isPositiveInteger:function (s){
		    var re = /^[0-9]+$/ ;
		    return re.test(s)
		  }, 
    	/*添加/修改*/
    	addFn:function(){
    		if(!this.data.value){
    			layer.alert("请填写值名称");
    			return;
    		}
    		
    		if(this.data.orderNumber&&!this.isPositiveInteger(this.data.orderNumber)){
    			layer.alert("排序号只能输入非负整数");
    			return;
    		}
    		if(this.currentIndex==1){
    			this.url="/system/addDataByParentId";
    		}else{
    			this.url="/system/updateData";
    			this.data.id=this.id;
    		}
    		this.$post(
    			this.url,
    			this.data
    		)
    		.then(response => {
    			/*未登录重定向*/
          if(response==444){
          	this.$router.push({path: '/login'});
          }else{
          	if (response.resultCode ==0) {
          		this.queryData();
          		if(this.currentIndex==1){
          			layer.alert("添加成功");
				    	}else{
				    		layer.alert("修改成功");
			    		}
          		this.OffModeBoxs();
	          }else{
	          	layer.alert(response.resultMsg);
	          }
          }
         
        }).catch(err => {
          console.log(err);
        });
    		$(".property_modal").css("display","none");
    	},
    	/*页码切换执行方法*/
			pageFn:function(val){
				this.model.page = parseInt(val);
				this.currentPage= parseInt(val);
				//此处调列表数据接口
				this.queryData();
			},
			/*搜索*/
    	searchList:function(){
    		this.model.page=1;
    		this.currentPage=1;
    		this.queryData();
    	},
    	/*获取列表*/
    	queryData:function(){
    		this.$post(
    			'/system/queryDataByParentId',
    			{
    				parentId:this.parentId,
    				value:this.searchValue,
    				"page":this.model.page,
	          "size":this.model.size,
    			}
    		)
    		.then(response => {
    			/*未登录重定向*/
          if(response==444){
          	this.$router.push({path: '/login'});
          }else{
          	if (response.resultCode ==0) {
          		this.tableList=response.resultData.sdList;
          		this.model.total=response.resultData.totalCount;
	          }else{
	          	layer.alert(response.resultMsg);
	          }
          }
         
        }).catch(err => {
          console.log(err);
        });
    	},
    	/*删除*/
			del:function(id){
				var _this=this;
				layer.confirm('您确定要删除这条数据吗？', {
					title:"删除",
					btnAlign:"c",
				  btn: ['确定','取消'] //按钮
				}, function(){
					_this.$get('/system/deleteDataByParentId/'+id)
	    		.then(response => {
	    			/*未登录重定向*/
	          if(response==444){
	          	_this.$router.push({path: '/login'});
	          }else{
	          	if (response.resultCode ==0) {
	          		layer.alert("删除成功");
	          		var num = Math.ceil(_this.model.total / _this.model.size)-1;
	          		if(_this.model.total-(_this.model.size*num)>1||_this.tableList.length>1){
	          			_this.model.page=_this.currentPage;
	          		}else if(_this.model.total==1){
	          			_this.model.page==1
	          		}else{
	          			_this.model.page=parseInt(_this.currentPage)-1;
	          		}
	          		_this.queryData();
		          }else{
		          	layer.alert(response.resultMsg);
		          }
	          }
	         
	        }).catch(err => {
	          console.log(err);
	        });
				});
			}
		}
  }
</script>
<style  lang="less" scoped>
	#addValue{
		.section-nav{
			height: 220px;
			p{
				margin-bottom: 20px;
				>span{
					width: 80px;
					display: inline-block;
					>span{
						color: red;
						margin-right: 4px;
					}
				}
				>select{
					width: 220px;
				}
				>.error{
					margin-left: 10px;
				}
			}
			div.btns{
				text-align: center;
				margin-top: 20px;
				
			}
		}
	}
	
</style>
